/*
 * Copyright 2012 SerGenis. All rights reserved.
 */
package com.sergenis.swing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Insets;
import javax.swing.JComponent;
import sun.swing.SwingUtilities2;

/**
 * @author SerGenis
 */
public class Size {

	public static Dimension fromColumns(JComponent component, int columns) {
		if (columns < 0)
			throw new IllegalArgumentException("columns less zero.");
		Dimension size = component.getPreferredSize();
		Font font = component.getFont();
		Insets insets = component.getInsets();
		FontMetrics metrics = SwingUtilities2.getFontMetrics(component, font);
		int columnWidth = metrics.charWidth('m');
		size.width = (columns * columnWidth) + insets.left + insets.right;
		return (size);
	}

	public static int widthForColumns(int columns, Font font) {
		JComponent component = new javax.swing.JLabel();
		component.setFont(font);
		FontMetrics metrics = SwingUtilities2.getFontMetrics(component, font);
		int columnWidth = metrics.charWidth('m');
		return (columns * columnWidth);
	}
}
